package cn.zswltech.gruul.config;

import cn.zswltech.gruul.web.api.intercept.SystemInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.filter.FormContentFilter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;


@EnableScheduling
@Configuration
@Slf4j
public class WebConfig implements WebMvcConfigurer {

    @Value("${gruul.system.intercept.excludepath:}")
    private String excludePath;
    /***
     * 格式化时间,默认为毫秒数
     */
//    @Override
//    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
//        builder.dateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
//        converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
//    }
    @Bean
    public HandlerInterceptor getSystemInterceptor() {
        return new SystemInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] configPatterns =StringUtils.isNotBlank(excludePath)? excludePath.split(";|,") : new String[0];
        String[] patterns = new String[]{ "/test","/manual/**","/user/getAuthCode","/ok", "/user/login", "/user/getAuthCode",
                "/user/checkToken","/user/logout", "/user/authCode","/user/dashboard/ssoLogin","/user/h5Cockpit/ssoLogin",
                "/user/portal/ssoLogin", "/regedit/menu", "/solution", "/user/todo/ssoLogin","/userCenter/getUserInfo",
                "/common/dict/enums"};
        int srclen = patterns.length;
        patterns = Arrays.copyOf(patterns, patterns.length + configPatterns.length);// 数组扩容
        System.arraycopy(configPatterns, 0, patterns, srclen, configPatterns.length);
        log.info("SystemInterceptor excludePathPatterns={}", Arrays.toString(patterns));
        //registry.addInterceptor(getSystemInterceptor()).addPathPatterns("/**").excludePathPatterns(patterns);
    }

    @Bean
    public FormContentFilter httpPutFormContentFilter() {
        return new FormContentFilter();
    }
}
